package com.xmsx.back.controller.admin;

import com.alibaba.fastjson.JSON;
import com.xmsx.back.pojo.Graduate;
import com.xmsx.back.results.Result;
import com.xmsx.back.service.admin.GraduateService;
import com.xmsx.back.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class GraduateController {
    @Autowired
    GraduateService graduateService;

    /**
     * 查询年份列表
     * @return
     */
    @GetMapping("/yearList")
    public Result<List<Integer>> getYearList(){
        List<Integer> yearList = graduateService.getYearList();
        if (!yearList.isEmpty()) {
            return Result.success(yearList);
        } else {
            return Result.error("不存在yearList");
        }
    }

    /**
     * 添加单条信息
     *
     * @param id
     * @param sex
     * @param nation
     * @param politicsSt
     * @param school
     * @param eduBackgro
     * @param major
     * @param origin
     * @param gradTime
     * @param difficulty
     * @param department
     * @param sid
     * @param direction
     * @param orgName
     * @param orgKind
     * @param orgIndustr
     * @param orgLocatio
     * @param jobType
     * @param orgAddress
     * @return
     */
    @PutMapping("/gradDirection")
    public Result<String> setGraduate(@RequestParam("id") String id,
                                      @RequestParam("sex") String sex,
                                      @RequestParam("nation") String nation,
                                      @RequestParam("politicsSt") String politicsSt,
                                      @RequestParam("school") String school,
                                      @RequestParam("eduBackgro") String eduBackgro,
                                      @RequestParam("major") String major,
                                      @RequestParam("origin") String origin,
                                      @RequestParam("gradTime") long gradTime,
                                      @RequestParam("difficulty") String difficulty,
                                      @RequestParam("department") String department,
                                      @RequestParam("sid") long sid,
                                      @RequestParam("direction") String direction,
                                      @RequestParam("orgName") String orgName,
                                      @RequestParam("orgKind") String orgKind,
                                      @RequestParam("orgIndustr") String orgIndustr,
                                      @RequestParam("orgLocatio") String orgLocatio,
                                      @RequestParam("jobType") String jobType,
                                      @RequestParam("orgAddress") String orgAddress) {
        Graduate graduate0 = graduateService.getById(id);

        if (null != graduate0) {
            // 已经存在，报错
            return Result.error("此条信息已经存在，新建失败");
        }

        Graduate graduate = new Graduate();
        graduate.setId(id);
        graduate.setSex(sex);
        graduate.setNation(nation);
        graduate.setPoliticsSt(politicsSt);
        graduate.setSchool(school);
        graduate.setEduBackgro(eduBackgro);
        graduate.setMajor(major);
        graduate.setOrigin(origin);
        graduate.setDifficulty(difficulty);
        graduate.setDepartment(department);
        graduate.setSid(sid);
        graduate.setGradTime(gradTime);
        graduate.setDirection(direction);
        graduate.setOrgName(orgName);
        graduate.setOrgKind(orgKind);
        graduate.setOrgIndustry(orgIndustr);
        graduate.setOrgLocation(orgLocatio);
        graduate.setJobType(jobType);
        graduate.setOrgAddress(orgAddress);

        graduateService.setGraduate(graduate);
        return Result.success("添加成功！");
    }

    /**
     * 修改单条信息
     *
     * @param id
     * @param sex
     * @param nation
     * @param politicsSt
     * @param school
     * @param eduBackgro
     * @param major
     * @param origin
     * @param gradTime
     * @param difficulty
     * @param department
     * @param sid
     * @param direction
     * @param orgName
     * @param orgKind
     * @param orgIndustr
     * @param orgLocatio
     * @param jobType
     * @param orgAddress
     * @return
     */
    @PostMapping("/gradDirection")
    public Result<String> updateGraduate(@RequestParam("id") String id,
                                         @RequestParam("sex") String sex,
                                         @RequestParam("nation") String nation,
                                         @RequestParam("politicsSt") String politicsSt,
                                         @RequestParam("school") String school,
                                         @RequestParam("eduBackgro") String eduBackgro,
                                         @RequestParam("major") String major,
                                         @RequestParam("origin") String origin,
                                         @RequestParam("gradTime") long gradTime,
                                         @RequestParam("difficulty") String difficulty,
                                         @RequestParam("department") String department,
                                         @RequestParam("sid") long sid,
                                         @RequestParam("direction") String direction,
                                         @RequestParam("orgName") String orgName,
                                         @RequestParam("orgKind") String orgKind,
                                         @RequestParam("orgIndustr") String orgIndustr,
                                         @RequestParam("orgLocatio") String orgLocatio,
                                         @RequestParam("jobType") String jobType,
                                         @RequestParam("orgAddress") String orgAddress) {
        Graduate graduate = new Graduate();
        graduate.setId(id);
        graduate.setSex(sex);
        graduate.setNation(nation);
        graduate.setPoliticsSt(politicsSt);
        graduate.setSchool(school);
        graduate.setEduBackgro(eduBackgro);
        graduate.setMajor(major);
        graduate.setOrigin(origin);
        graduate.setDifficulty(difficulty);
        graduate.setDepartment(department);
        graduate.setSid(sid);
        graduate.setGradTime(gradTime);
        graduate.setDirection(direction);
        graduate.setOrgName(orgName);
        graduate.setOrgKind(orgKind);
        graduate.setOrgIndustry(orgIndustr);
        graduate.setOrgLocation(orgLocatio);
        graduate.setJobType(jobType);
        graduate.setOrgAddress(orgAddress);

        graduateService.updateGraduate(graduate);
        return Result.success("修改成功！");
    }

    /**
     * 删除单条信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/gradDirection")
    public Result<String> deleteGraduate(@RequestParam("id") String id) {
        Graduate graduate = graduateService.getById(id);
        if (null == graduate) {
            // 信息不存在，无法删除
            return Result.error("毕业信息不存在，删除失败");
        }
        graduateService.deleteGraduate(id);
        return Result.success("删除成功！");
    }

    /**
     * 搜索——根据考生/学号获取信息
     *
     * @param xid
     * @return
     */
    @GetMapping("/gradDirection")
    public Result<Graduate> getGraduateById(@RequestParam("xid") String xid) {
        Graduate graduate = graduateService.getById(xid);
        if (graduate != null) {
            return Result.success(graduate);
        } else {
            Graduate graduate1 = graduateService.getBySid(xid);
            if (graduate1 != null) {
                return Result.success(graduate1);
            }
            return Result.error("不存在该条信息");
        }

    }

    /**
     * 批量导入多条信息
     *
     * @param info
     * @return
     */
    @PutMapping("/gradDirectionExcel")
    public Result<String> setGraduate(@RequestBody String info) {
        System.out.println("接收到的json数据  "+info);
        List<Graduate> graduateList = JSON.parseArray(info, Graduate.class);
        return  graduateService.batchInsert(graduateList);
    }

    /**
     * 获取分页的多条毕业信息
     *
     * @param pageCount
     * @param size
     * @return
     */
    @GetMapping("/gradDirectionList")
    public Result<Map> getGraduateList(@RequestParam("pageCount") int pageCount,
                                       @RequestParam("size") int size,
                                       @RequestParam("start") int start,
                                       @RequestParam("end") int end,
                                       @RequestParam("edu") String edu) {
        Pager pager = graduateService.findByPager(pageCount, size, start, end, edu);
        Map myMap = new HashMap<String, String>();
        myMap.put("total", pager.getTotal());
        myMap.put("list", pager.getRows());
        return Result.success(myMap);
    }
}
